package net.dgg.zz.production.org.controller;

import net.dgg.framework.tac.utils.bean.DggRestResponse;
import net.dgg.framework.tac.utils.web.commmon.DggBaseController;
import net.dgg.iboss.base.service.CmsService;
import net.dgg.iboss.base.util.CustomerNumberEncryptionUtil;
import net.dgg.iboss.base.util.DesUtil;
import net.dgg.iboss.base.util.datatable.DataTableUtils;
import net.dgg.iboss.base.util.datatable.domain.DataTableRequest;
import net.dgg.iboss.base.util.datatable.domain.DataTableResponse;
import net.dgg.tmd.foundation.platform.common.util.TranslateUtil;
import net.dgg.tmd.foundation.platform.session.SessionManager;
import net.dgg.tmd.foundation.platform.user.entity.UserEntity;
import net.dgg.zz.common.constant.ScProductConstant;
import net.dgg.zz.common.dao.OrderDao;
import net.dgg.zz.common.entity.Order;
import net.dgg.zz.common.service.CommonMangerExtService;
import net.dgg.zz.common.utils.OrderHandleUtils;
import net.dgg.zz.production.distribution.service.DistributionService;
import net.dgg.zz.production.entity.ProductOrder;
import net.dgg.zz.production.inProcess.dao.ProductOrderDao;
import net.dgg.zz.production.inProcess.entity.Assistant;
import net.dgg.zz.production.inProcess.service.ZzInProcessService;
import net.dgg.zz.production.org.service.OrgOrderService;
import net.fblock.log.NLog;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 事业部订单
 * <p>@Package: net.dgg.zz.production</p>
 * <p>@Title：OrgController</p>
 * <p>@description:</p>
 * <p>@author zhangwen</p>
 * <p>@date 2018年9月14日</p>
 * <p>@Company dgg</p>
 */

@Controller
@RequestMapping("sc/org")
public class OrgController extends DggBaseController {
	
	
	@Autowired
    private OrgOrderService orgOrderService;
    @Autowired
    private CmsService cmsService;
    @Autowired
    private TranslateUtil translateUtil;
    
    @Autowired
    private DesUtil desUtil;
    @Autowired
    private ProductOrderDao productOrderDao;
    @Autowired
    private OrderDao orderDao;
    @Autowired
    private SessionManager sessionmanager;
    @Autowired
    private CommonMangerExtService commonMangerExtService;

    @Autowired
    DistributionService distributionService;

    @Autowired
    private ZzInProcessService zzInProcessService;


    /**
     * 事业部订单列表
     * @return
     */
    @NLog
    @RequestMapping(value = "/orgOrder")
    public String orgOrder(Model model,String status) {
        return "zzsc/org/org_order";
    }

    /**
     * json查询事业部生产订单
     * @param params
     * @param request
     */
    @SuppressWarnings({ "unchecked", "rawtypes" })
    @NLog
    @RequestMapping(value = "/orgOrderJson")
    @ResponseBody
    public DataTableResponse orgOrderJson(@RequestParam Map params, HttpServletRequest request) {
        DataTableRequest r = DataTableUtils.getParam(request);
        params.put("page", r.getPage());
        params.put("limit", r.getLength());
        Map fileMap = new HashMap();
        fileMap.put("signTime", "signTime");
        OrderHandleUtils.getOrderCol(r, params, fileMap);
        List<Map> list = orgOrderService.orgOrderWithPage(params);
        String[] transColum = {"originCode","status"};
        list = translateUtil.translateList(transColum, list);
        String[] hideColum = {"customerPhone"};//隐藏
        list = desUtil.decryptListMap(hideColum, list);
        CustomerNumberEncryptionUtil.encryptionList(hideColum,list);
        int count = Integer.parseInt(params.get("count").toString());//总条数
        return new DataTableResponse(r.getDraw(), count, count, list, "");
    }


    /**
     * 移交页面
     *
     */
    @NLog
    @RequestMapping(value = "/transfer_org_order_page")
    public String transferOrgOrderPage(Model model,Long id) {

        Order order = orgOrderService.selectByPrimaryKey(id);
        String customerPhone = desUtil.decrypt(order.getCustomerPhone());
        order.setCustomerPhone(customerPhone);
        model.addAttribute("order",order);
        Map map = orgOrderService.queryProductByOrderId(id);
        model.addAttribute("productNames",map.get("productNames").toString());
        String[] transColum = {"originCode","status"};
        List list = (List) map.get("list");
        list = translateUtil.translateList(transColum, list);
        String[] hideColum = {"customerPhone"};//隐藏
        list = desUtil.decryptListMap(hideColum, list);
        list = CustomerNumberEncryptionUtil.encryptionList(hideColum,list);
        model.addAttribute("list",list);

        return "zzsc/org/transfer_org_order_page";
    }

    /**
     * 确认移交
     * @param params
     * @return
     */
    @SuppressWarnings({ "unchecked", "rawtypes" })
    @NLog
    @RequestMapping(value = "/sure_transfer")
    @ResponseBody
    public DggRestResponse createNewNodeFlowSave(@RequestParam Map params) {
        try {
            UserEntity userentity = sessionmanager.getCurrentUser();
            Map orgMap = commonMangerExtService.findBusDeptById(userentity.getOrgId());
            if(orgMap.get("id").toString().equals(params.get("orgId").toString())){
                return this.getFailResponse("同事业部不能移交！");
            }
            orgOrderService.sureTransfer(params);
            return this.getSuccessResponse("保存成功");
        } catch (Exception e) {
            e.printStackTrace();
            return this.getFailResponse(e.getMessage());
        }

    }


    /**
     * 事业部订单待分配
     * @return
     */
    @NLog
    @RequestMapping(value = "/orgOrderWaitAllot")
    public String orgOrderWaitAllot(Model model,String status) {
        return "zzsc/org/org_order_wait_allot";
    }

    /**
     * json查询事业部订单待分配
     * @param params
     * @param request
     */
    @SuppressWarnings({ "unchecked", "rawtypes" })
    @NLog
    @RequestMapping(value = "/orgOrderWaitAllotJson")
    @ResponseBody
    public DataTableResponse orgOrderWaitAllotJson(@RequestParam Map params, HttpServletRequest request) {
        DataTableRequest r = DataTableUtils.getParam(request);
        params.put("page", r.getPage());
        params.put("limit", r.getLength());
        Map fileMap = new HashMap();
        fileMap.put("transferTime", "transferTime");
        fileMap.put("signTime", "signTime");
        OrderHandleUtils.getOrderCol(r, params, fileMap);
        if(null!=params.get("startTransferDate")){
            params.put("startTransferDate",params.get("startTransferDate").toString()+" 00:00:00");
        }
        if(null!=params.get("endTransferDate")){
            params.put("endTransferDate",params.get("endTransferDate").toString()+" 23:59:59");
        }
        if(null!=params.get("startSignDate")){
            params.put("startSignDate",params.get("startSignDate").toString()+" 00:00:00");
        }
        if(null!=params.get("endSignDate")){
            params.put("endSignDate",params.get("endSignDate").toString()+" 23:59:59");
        }
        List<Map> list = orgOrderService.orgOrderWaitAllotWithPage(params);
        String[] hideColum = {"customerPhone"};//隐藏
        list = desUtil.decryptListMap(hideColum, list);
        list = CustomerNumberEncryptionUtil.encryptionList(hideColum,list);
        int count = Integer.parseInt(params.get("count").toString());//总条数
        return new DataTableResponse(r.getDraw(), count, count, list, "");
    }

    /**
     * 分配页面
     *
     */
    @NLog
    @RequestMapping(value = "/allot_org_order_page")
    public String allotOrgOrderPage(Model model,Long id) {

        ProductOrder po = productOrderDao.selectByPrimaryKey(id);
        Order order = orderDao.selectByOrderId(po.getOrderId());
        String customerPhone = desUtil.decrypt(order.getCustomerPhone());
        order.setCustomerPhone(customerPhone);
        model.addAttribute("order",order);
        Map map = orgOrderService.queryProductByOrderId(order.getId());
        model.addAttribute("productNames",map.get("productNames").toString());
        String[] transColum = {"originCode","status"};
        List list = (List) map.get("list");
        list = translateUtil.translateList(transColum, list);
        String[] hideColum = {"customerPhone"};//隐藏
        list = desUtil.decryptListMap(hideColum, list);
        list = CustomerNumberEncryptionUtil.encryptionList(hideColum,list);
        model.addAttribute("list",list);

        model.addAttribute("ALLOT_TO_FLOW_USER", ScProductConstant.ALLOT_TO_FLOW_USER);
        model.addAttribute("ALLOT_TO_PRODUCT_USER", ScProductConstant.ALLOT_TO_PRODUCT_USER);
        model.addAttribute("productOrderInfo",distributionService.selectByPrimaryKey(id));
        //获取协单人员数据
        //获取协单人员数据
        StringBuilder ids = new StringBuilder();
        List<Assistant> scAssistantList = zzInProcessService.queyScAssistantList(id);
        if (scAssistantList != null && !scAssistantList.isEmpty()) {
            for (Assistant scAssistant : scAssistantList) {
                ids.append(scAssistant.getAssistUserId().toString().concat(","));
            }
            model.addAttribute("ids", ids.deleteCharAt(ids.lastIndexOf(",")));
        }
        return "zzsc/org/allot_org_order_page";
    }


    /**
     * 事业部移交记录
     * @return
     */
    @NLog
    @RequestMapping(value = "/orgOrderTransferRecord")
    public String orgOrderTransferRecord(Model model,String status) {
        return "zzsc/org/org_order_transfer_record";
    }

    /**
     * json查询事业部移交记录
     * @param params
     * @param request
     */
    @SuppressWarnings({ "unchecked", "rawtypes" })
    @NLog
    @RequestMapping(value = "/orgOrderTransferRecordJson")
    @ResponseBody
    public DataTableResponse orgOrderTransferRecordJson(@RequestParam Map params, HttpServletRequest request) {
        DataTableRequest r = DataTableUtils.getParam(request);
        if(null!=params.get("startTransferDate")){
            params.put("startTransferDate",params.get("startTransferDate").toString()+" 00:00:00");
        }
        if(null!=params.get("endTransferDate")){
            params.put("endTransferDate",params.get("endTransferDate").toString()+" 23:59:59");
        }
        if(null!=params.get("startSignDate")){
            params.put("startSignDate",params.get("startSignDate").toString()+" 00:00:00");
        }
        if(null!=params.get("endSignDate")){
            params.put("endSignDate",params.get("endSignDate").toString()+" 23:59:59");
        }
        params.put("page", r.getPage());
        params.put("limit", r.getLength());

        Map fileMap = new HashMap();
        fileMap.put("signTime", "signTime");
        fileMap.put("transferTime", "transferTime");
        OrderHandleUtils.getOrderCol(r, params, fileMap);
        List<Map> list = orgOrderService.orgOrderTransferRecordWithPage(params);
        String[] hideColum = {"customerPhone"};//隐藏
        list = desUtil.decryptListMap(hideColum, list);
        list = CustomerNumberEncryptionUtil.encryptionList(hideColum,list);
        int count = Integer.parseInt(params.get("count").toString());//总条数
        return new DataTableResponse(r.getDraw(), count, count, list, "");
    }

}
